package com.jyserver.common.AutoCode.jybaits;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class GeneratorWeb {

    public void GeneratorWeb(IntrospectedTable arg1, String GeneratorWeb_targetPackage) {
        String[] paths = GeneratorWeb_targetPackage.replace(".", "/").split("/");
        String path = "";
        for (int i = 0; i < paths.length; i++) {
            path = path + "/" + paths[i];
            File file = new File(path);
            if (!file.exists()) {
                file.mkdir();
            }
        }
        Style_two2(arg1, GeneratorWeb_targetPackage);
    }


    /**
     * @Description :样式1
     */
    public void Style_noe(IntrospectedTable arg1, String GeneratorWeb_targetPackage) {
        try {
            List<IntrospectedColumn> introspectedColumns = arg1.getAllColumns();
            String str = "";
            String strids = "";
            for (int i = 0; i < introspectedColumns.size(); i++) {
                strids = strids + introspectedColumns.get(i).getActualColumnName() + ":0,\n";
                str = str + " <div class=\"layui-form-item\">\n" +
                        "        <label class=\"layui-form-label\"style=\"width: 60%\">" + introspectedColumns.get(i).getChinese() + "</label>\n" +
                        "            <input type=\"text\" name=\"" + introspectedColumns.get(i).getActualColumnName() + "\" v-model=\"fromdata." + introspectedColumns.get(i).getActualColumnName() + "\" value=\"0\" placeholder=\"請輸入" + introspectedColumns.get(i).getChinese() + "\" autocomplete=\"off\" class=\"layui-input\" style=\"width: 40%\">\n" +
                        "    </div>\n";
            }
            FileWriter fw = new FileWriter(GeneratorWeb_targetPackage + "/" + arg1.getChinese() + ".html");
            fw.write(str + strids);
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Description :样式1
     */
    public void Style_two(IntrospectedTable arg1, String GeneratorWeb_targetPackage) {
        try {
            List<IntrospectedColumn> introspectedColumns = arg1.getAllColumns();
            String str = "";
            String strids = "";
            String str2="";
            String str3="";
            for (int i = 0; i < introspectedColumns.size(); i++) {
                String name=introspectedColumns.get(i).getActualColumnName();
                String  ends="LessThan";
                if (name.indexOf("ess")>0){
                    ends="GreaterThan";
                }
                String[] names=name.split("_");
                String name1=names[0];
                String name2=names[0];
                for (int k=1;k<names.length;k++){
                    name1=name1+toUpperCaseFirstOne(names[k]);
                }
                str2=str2+"<td>"+introspectedColumns.get(i).getChinese()+"</td>\n";
                str3=str3+"<td>{{site."+name1+"}}</td>\n";
                name2=name1.replace("Less","").replace("Than","");
                str=str+" if (HelpUtils.isNotEmpty(realpriceparams.get"+name1+"()) && Double.valueOf(realpriceparams.get"+name1+"()) != 0) {\n" +
                        "                realpricehistoryviewexample.or().and"+name2+ends+"(Double.valueOf(realpriceparams.get"+name1+"()));\n" +
                        "            }\n";
            }
            FileWriter fw = new FileWriter(GeneratorWeb_targetPackage + "/" + arg1.getChinese() + "s.html");
            fw.write(str2 + str3);
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String toUpperCaseFirstOne(String s){
        if(Character.isUpperCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
    }

    /**
     * @Description :样式1
     */
    public void Style_two2(IntrospectedTable arg1, String GeneratorWeb_targetPackage) {

        try {
            List<IntrospectedColumn> introspectedColumns = arg1.getAllColumns();
            String str = "";
            String strids = "";
            String str2="";
            String str3="";
            if (arg1.getChinese().indexOf("query")!=-1){
                str3="CREATE TRIGGER \""+arg1.getChinese()+"_create_time\" BEFORE INSERT ON \"public\".\""+arg1.getChinese()+"\"\n" +
                        "FOR EACH ROW\n" +
                        "EXECUTE PROCEDURE \"public\".\"date_create_time\"();";


                FileWriter fw = null;
                try {
//如果文件存在，则追加内容；如果文件不存在，则创建文件
                    File f=new File("E:\\myselfProject\\StockServer\\src\\main\\resources\\sql\\query2.sql");
                    fw = new FileWriter(f, true);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                PrintWriter pw = new PrintWriter(fw);
                pw.println(str3);
                pw.flush();
                try {
                    fw.flush();
                    pw.close();
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

//                FileWriter fw = new FileWriter("E:\\myselfProject\\StockServer\\src\\main\\resources\\sql\\query2.sql");
//                fw.append(str3);
//                fw.close();
                return;
            }
            for (int i = 0; i < introspectedColumns.size(); i++) {
                String name=introspectedColumns.get(i).getActualColumnName();
                String  ends="LessThan";
                if (name.indexOf("ess")>0){
                    ends="GreaterThan";
                }
                String[] names=name.split("_");
                String name1=names[0];
                String name2=names[0];
                for (int k=1;k<names.length;k++){
                    name1=name1+toUpperCaseFirstOne(names[k]);
                }
                str2=str2+"<td>"+introspectedColumns.get(i).getChinese()+"</td>\n";
                str3=str3+"{\nfield: '"+ name1+"', \ntitle:'"+introspectedColumns.get(i).getChinese()+"', \nwidth: 100, \ntitleAlign: 'center',\n formatter: function (rowData, index) {\n" +
                        "         value = Number(rowData."+name1+");\n" +
                        "         if (value > 10000 || value < -10000) {\n" +
                        "        value = (value / 10000).toFixed(2) + \"万\";\n" +
                        "        }else {\n" +
                        "                                value=value.toFixed(3)\n" +
                        "                            }\n" +
                        "        return value;\n" +
                        "        },\ncolumnAlign:'center'\n},\n";
                name2=name1.replace("Less","").replace("Than","");
                str=str+" if (HelpUtils.isNotEmpty(realpriceparams.get"+name1+"()) && Double.valueOf(realpriceparams.get"+name1+"()) != 0) {\n" +
                        "                realpricehistoryviewexample.or().and"+name2+ends+"(Double.valueOf(realpriceparams.get"+name1+"()));\n" +
                        "            }\n";
            }
            FileWriter fw = new FileWriter(GeneratorWeb_targetPackage + "/" + arg1.getChinese() + "s.html");
            fw.write( str3);
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}





